package com.woniuxy.util;


import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.woniuxy.core.exception.WoniuxyException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class MyAlipayUtil {
    @Resource
    private AlipayClient alipayClient;

    /**
     * 网页支付方法 参数:封装支付参数，需要包含订单号、支付金额、支付说明、详细描述（可选）、同步回调地址、异步回调地址
     */
    //发起支付
    public String pagePay(PagePayParam param) throws Exception {
        // 实例化网页支付请求对象
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 设置参数
        String content = "{\"out_trade_no\":\"" + param.getOrderId() + "\","
                + "\"total_amount\":\"" + param.getMoney() + "\","
                + "\"subject\":\"" + param.getSubject() + "\","
                + "\"body\":\"" + param.getBody() + "\","
                + "\"timeout_express\":\"" + "5m" + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}";
        request.setBizContent(content);
        //设置同步回调和异步回调
        request.setReturnUrl(param.getReturnUrl());
        request.setNotifyUrl(param.getNotifyUrl());
        //执行网页支付请求
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        //判断支付宝响应结果
        if (response.isSuccess()) {
            //返回支付宝响应的支付表单html内容
            return response.getBody();
        }
        throw new WoniuxyException(500, "发起支付失败");
    }

    //查询支付订单信息
    public PayInfoDto selectPayInfo(String orderNum) throws Exception {
        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
        //请二选一设置
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderNum + "\"}");
        //请求
        String result = alipayClient.execute(alipayRequest).getBody();

        String payInfo = JSONUtil.parseObj(result).get("alipay_trade_query_response").toString();
        String payCode = JSONUtil.parseObj(payInfo).get("code").toString();

        if ("10000".equals(payCode)) {
            String payDate = JSONUtil.parseObj(payInfo).get("send_pay_date").toString();
            String payState = JSONUtil.parseObj(payInfo).get("trade_status").toString();
            PayInfoDto dto = new PayInfoDto(payDate, payState);
            return dto;
        } else {
            PayInfoDto dto = new PayInfoDto(null, "TRADE_FAIL");
            return dto;
        }
    }

//    public  boolean QuitMoney(String orderNum) throws Exception {
//        //设置请求参数
//        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
//        GoodsOrder goodsOrder = goodsOrderMapper.selectById(orderNum);
//        String orderPrice = goodsOrder.getGoodsOrderPrice().toString();
//        String reason = "用户取消了购买";
//        alipayRequest.setBizContent("{\"out_trade_no\":\""+ orderNum +"\","
//                + "\"refund_amount\":\""+ orderPrice +"\","
//                + "\"refund_reason\":\""+ reason
//                +"\"}");
//        String result = alipayClient.execute(alipayRequest).getBody();
//        String order = JSONUtil.parseObj(result).get("alipay_trade_refund_response").toString();
//        return "10000".equals( JSONUtil.parseObj(order).get("code"));
//    }

}
